home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / hdf3_2r2.lha / HDF3.2r2 / util / hdf24to8.c next >
Encoding:
C/C++ Source or Header  |  1992-10-28  |  8.5 KB  |  378 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. #ifdef RCSID
  26. static char RcsId[] = "@(#)$Revision: 1.2 $";
  27. #endif
  28. /*
  29. $Header: /hdf/hdf/v3.2r2/util/RCS/hdf24to8.c,v 1.2 1992/07/15 21:48:48 sxu beta koziol $
  30.  
  31. $Log: hdf24to8.c,v $
  32.  * Revision 1.2  1992/07/15  21:48:48  sxu
  33.  * Added changes for CONVEX
  34.  *
  35.  * Revision 1.1  1992/06/09  17:35:18  mfolk
  36.  * Initial revision
  37.  *
  38. */
  39.  
  40. /**************************************************************************
  41. * hdf24hdf8    Quantizes a HDF RGB 24 bit "pixel" image into a 8 bit image
  42. *               with RGB palette and stores it as a HDF 8-bit raster image
  43. *               file.
  44. *
  45. *  usage:        hdf24hdf8 r24_file hdf8_file
  46. *
  47. *               On Input:
  48. *               --------
  49. *
  50. *               hdf24_file    - File containing the HDF RGB 24-bit
  51. *                             raster image.
  52. *
  53. *               On Output:
  54. *               ---------
  55. *
  56. *               hdf8_file    - HDF file with one 8-bit raster image set,
  57. *                             i.e. 8-bit image, dimensions, and 
  58. *                             RGB palette.
  59. *
  60. * by:            NCSA
  61. * date(s):        May 89, Jun 89, Aug 89, May 90
  62. *
  63. ****************************************************************************/
  64.  
  65. typedef    unsigned char    UCHAR;
  66. typedef unsigned int    UINT;
  67.  
  68. #define        NCOLORS        256
  69. #define        PALSIZE        3 * NCOLORS
  70. #define        COMPRESSION    0        /* no compression */
  71.  
  72. #include <stdio.h>
  73.  
  74. #ifdef UNIX
  75. #include <sys/file.h>
  76. #endif
  77. #ifdef MAC
  78. #include <FCntl.h>
  79. #include <StdLib.h>
  80. #endif
  81.  
  82. #include "hdf.h"
  83.  
  84. #define USAGE    fprintf (stderr, "usage: hdf24hdf8 hdf24_file hdf8_file\n")
  85.  
  86. int main
  87.   PROTO((int, char **));
  88. int r24r8
  89.   PROTO((int, int, unsigned char *, unsigned char *, int, unsigned char *));
  90.  
  91. #ifdef PROTOTYPE
  92. main (int argc, char *argv[])
  93. #else
  94. main (argc, argv)
  95. int argc;
  96. char *argv[];
  97. #endif /* PROTOTYPE */ 
  98. {
  99.     int i, nc;
  100.     int32 x_dim, y_dim, size;
  101.     int interlace;
  102.     char c;
  103.     char *ptr;
  104.     uint8 *r24, *r8, *pal;
  105.     uint8 hdfpal[PALSIZE], *p;
  106. #ifdef UNIX
  107.     char *malloc ();
  108. #endif
  109.  
  110.     if (argc != 3)
  111.     {
  112.         USAGE;
  113.         exit (1);
  114.     }
  115.  
  116.     /* Get the HDF R24 image */
  117.  
  118.     if (DF24getdims(argv[1], &x_dim, &y_dim, &interlace) < 0) {
  119.         fprintf(stderr,"error: %s is not an HDF file or ",
  120.             argv[1]);
  121.         fprintf(stderr,"it does not contain a R24 image\n");
  122.         exit (-1);
  123.     }
  124.  
  125.     size = x_dim * y_dim;
  126.  
  127.     if ((r24 = (UCHAR *) malloc (size * 3)) == NULL)
  128.     {
  129.         fprintf (stderr, "error: malloc to hold r24 image failed\n");
  130.         exit (-1);
  131.     }
  132.  
  133.     if (DF24getimage(argv[1],(VOIDP) r24, x_dim, y_dim) < 0) {
  134.         fprintf(stderr,"error: DF24getimage failed\n");
  135.         exit (-1);
  136.     }
  137.  
  138.     if ((r8 = (UCHAR *) malloc (size)) == NULL)
  139.     {
  140.         fprintf (stderr, "error: malloc to hold r8 image failed\n");
  141.         exit (-1);
  142.     }
  143.     if ((pal = (UCHAR *) malloc (PALSIZE)) == NULL)
  144.     {
  145.         fprintf (stderr, "error: malloc to hold palette failed\n");
  146.         exit (-1);
  147.     }
  148.  
  149.     if (r24r8 (x_dim, y_dim, r24, r8, NCOLORS, pal) == -1)
  150.     {
  151.         fprintf (stderr, "error: quantization failed\n");
  152.         exit (-1);
  153.     }
  154.  
  155.         /* rearrange palette to conform to HDF requirements */
  156.     p = hdfpal;
  157.     for (i = 0; i < NCOLORS; i++)
  158.     {
  159.         *p++ = pal[i];
  160.         *p++ = pal[i + NCOLORS];
  161.         *p++ = pal[i + NCOLORS * 2];
  162.     }
  163.     if (DFR8setpalette (hdfpal) == -1)
  164.     {
  165.         HEprint(stderr,0);
  166.         exit (-1);
  167.     }
  168.     if (DFR8putimage (argv[2], (VOIDP) r8, x_dim,y_dim,  COMPRESSION) == -1)
  169.     {
  170.         HEprint(stderr,0);
  171.         exit (-1);
  172.     }
  173.  
  174.     free ((char *) r24);
  175.     free ((char *) r8);
  176.     free ((char *) pal);
  177.  
  178.     return 0;
  179. }
  180.  
  181. #ifdef PROTOTYPE
  182. r24r8 (int xres, int yres, UCHAR *dat24, UCHAR *dat8, int cres, UCHAR *cdat)
  183. #else
  184. r24r8 (xres, yres, dat24, dat8, cres, cdat)
  185. int      xres;        /* x dimension - horizontal size */
  186. int      yres;        /* y dimension - vertical size */
  187. UCHAR   *dat24;        /* pointer to 24 bit image in "pixel" format */
  188. UCHAR   *dat8;        /* pointer to 8 bit image */
  189. int      cres;        /* number of colors in the palette - use 256 */
  190. UCHAR   *cdat;        /* pointer to palette - should be 3 * 256 bytes long */
  191. #endif /* PROTOTYPE */ 
  192. {
  193.     int      ct,xct,yct;
  194.     int      rres,rd,rr,rn,rct;
  195.     int      gres,gd,gr,gn,gct;
  196.     int      bres,bd,br,bn,bct;
  197.     int      coff;
  198.     UINT    *idat[2];
  199.     UINT    *cp,*np;
  200.     UCHAR   *dip,*dop,*rp,*gp,*bp;
  201. #ifdef UNIX
  202.     char    *malloc();
  203. #endif
  204.  
  205.     if ((idat[0] = (UINT *)malloc(6*xres*sizeof(UINT))) == NULL)
  206.     {   fprintf(stderr,"error: Memory allocation fault\n");
  207.     return -1;
  208.     }
  209.     idat[1] = idat[0] + (3 * xres);
  210.  
  211.     rres = 6;
  212.     gres = 7;
  213.     bres = 6;
  214.     coff = 2;
  215.  
  216.     rr = gr = br = 255;
  217.     rn = rres - 1;
  218.     gn = gres - 1;
  219.     bn = bres - 1;
  220.  
  221.     rp = cdat + coff;
  222.     gp = rp + cres;
  223.     bp = gp + cres;
  224.  
  225.     for (rct=0; rct<rres; rct++)
  226.     {   for (gct=0; gct<gres; gct++)
  227.     {   for (bct=0; bct<bres; bct++)
  228.         {   *rp++ = (UCHAR)(rr * rct / rn);
  229.         *gp++ = (UCHAR)(gr * gct / gn);
  230.         *bp++ = (UCHAR)(br * bct / bn);
  231.         }
  232.     }
  233.     }
  234.  
  235.     rp = cdat;
  236.     gp = rp + cres;
  237.     bp = gp + cres;
  238.     cp = idat[0];
  239.     np = idat[1];
  240.     dip = dat24;
  241.     dop = dat8;
  242.  
  243.     for (xct=3*xres; --xct>=0; )
  244.     *cp++ = *dip++;
  245.  
  246.     for (yct=0; yct<(yres-1); yct++)
  247.     {
  248.     np = idat[(yct+1)%2];
  249.     for (xct=3*xres; --xct>=0; )
  250.         *np++ = *dip++;
  251.  
  252.     cp = idat[yct%2];
  253.     np = idat[(yct+1)%2];
  254.  
  255.     if ((rct = (cp[0] * rn / rr)) > rn) rct = rn;
  256.     if ((gct = (cp[1] * gn / gr)) > gn) gct = gn;
  257.     if ((bct = (cp[2] * bn / br)) > bn) bct = bn;
  258.  
  259.     *dop++ = ct = (rct * gres + gct) * bres + bct + coff;
  260.  
  261.     rd = cp[0] - rp[ct];
  262.     gd = cp[1] - gp[ct];
  263.     bd = cp[2] - bp[ct];
  264.  
  265.     cp += 3;
  266.     np += 3;
  267.  
  268.     cp[0]  += rd * 7 / 16;
  269.     cp[1]  += gd * 7 / 16;
  270.     cp[2]  += bd * 7 / 16;
  271.     np[-3] += rd * 5 / 16;
  272.     np[-2] += gd * 5 / 16;
  273.     np[-1] += bd * 5 / 16;
  274.     np[0]  += rd / 16;
  275.     np[1]  += gd / 16;
  276.     np[2]  += bd / 16;
  277.  
  278.     for (xct=2; xct<xres; xct++)
  279.     {
  280.         if ((rct = (cp[0] * rn / rr)) > rn) rct = rn;
  281.         if ((gct = (cp[1] * gn / gr)) > gn) gct = gn;
  282.         if ((bct = (cp[2] * bn / br)) > bn) bct = bn;
  283.  
  284.         *dop++ = ct = (rct * gres + gct) * bres + bct + coff;
  285.  
  286.         rd = cp[0] - rp[ct];
  287.         gd = cp[1] - gp[ct];
  288.         bd = cp[2] - bp[ct];
  289.  
  290.         cp += 3;
  291.         np += 3;
  292.  
  293.         cp[0]  += rd * 7 / 16;
  294.         cp[1]  += gd * 7 / 16;
  295.         cp[2]  += bd * 7 / 16;
  296.         np[-6] += rd * 3 / 16;
  297.         np[-5] += gd * 3 / 16;
  298.         np[-4] += bd * 3 / 16;
  299.         np[-3] += rd * 5 / 16;
  300.         np[-2] += gd * 5 / 16;
  301.         np[-1] += bd * 5 / 16;
  302.         np[0]  += rd / 16;
  303.         np[1]  += gd / 16;
  304.         np[2]  += bd / 16;
  305.  
  306.     }
  307.  
  308.     if ((rct = (cp[0] * rn / rr)) > rn) rct = rn;
  309.     if ((gct = (cp[1] * gn / gr)) > gn) gct = gn;
  310.     if ((bct = (cp[2] * bn / br)) > bn) bct = bn;
  311.  
  312.     *dop++ = ct = (rct * gres + gct) * bres + bct + coff;
  313.  
  314.     rd = cp[0] - rp[ct];
  315.     gd = cp[1] - gp[ct];
  316.     bd = cp[2] - bp[ct];
  317.  
  318.     cp += 3;
  319.     np += 3;
  320.  
  321.     np[-6] += rd * 3 / 16;
  322.     np[-5] += gd * 3 / 16;
  323.     np[-4] += bd * 3 / 16;
  324.     np[-3] += rd * 5 / 16;
  325.     np[-2] += gd * 5 / 16;
  326.     np[-1] += bd * 5 / 16;
  327.     }
  328.  
  329.     cp = idat[yct%2];
  330.  
  331.     if ((rct = (cp[0] * rn / rr)) > rn) rct = rn;
  332.     if ((gct = (cp[1] * gn / gr)) > gn) gct = gn;
  333.     if ((bct = (cp[2] * bn / br)) > bn) bct = bn;
  334.  
  335.     *dop++ = ct = (rct * gres + gct) * bres + bct + coff;
  336.  
  337.     rd = cp[0] - rp[ct];
  338.     gd = cp[1] - gp[ct];
  339.     bd = cp[2] - bp[ct];
  340.  
  341.     cp += 3;
  342.  
  343.     cp[0]  += rd * 7 / 16;
  344.     cp[1]  += gd * 7 / 16;
  345.     cp[2]  += bd * 7 / 16;
  346.  
  347.     for (xct=2; xct<xres; xct++)
  348.     {
  349.     if ((rct = (cp[0] * rn / rr)) > rn) rct = rn;
  350.     if ((gct = (cp[1] * gn / gr)) > gn) gct = gn;
  351.     if ((bct = (cp[2] * bn / br)) > bn) bct = bn;
  352.  
  353.     *dop++ = ct = (rct * gres + gct) * bres + bct + coff;
  354.  
  355.     rd = cp[0] - rp[ct];
  356.     gd = cp[1] - gp[ct];
  357.     bd = cp[2] - bp[ct];
  358.  
  359.     cp += 3;
  360.  
  361.     cp[0]  += rd * 7 / 16;
  362.     cp[1]  += gd * 7 / 16;
  363.     cp[2]  += bd * 7 / 16;
  364.     }
  365.  
  366.     if ((rct = (cp[0] * rn / rr)) > rn) rct = rn;
  367.     if ((gct = (cp[1] * gn / gr)) > gn) gct = gn;
  368.     if ((bct = (cp[2] * bn / br)) > bn) bct = bn;
  369.  
  370.     *dop++ = (rct * gres + gct) * bres + bct + coff;
  371.  
  372.     free(idat[0]);
  373.     return 0;
  374. }
  375.  
  376.  
  377.  
  378.